<?php
/**
 * 原点 2020年7月14日17:47:36 提现相关
 */

namespace app\index\model;

use app\common\model\Account;
use app\common\model\Logs;
use think\Db;
use think\Model;



class Draw extends Model
{
    protected $name = 'draw';
    protected $dateFormat = 'm-d H:i';

    protected function base($query){
		$query->where(['is_del'=>'0']);
	}

    public function get_list($page, $uid)
    {
        try {
            return $this->where('uid', $uid)->page($page, 10)->order('create_time','desc')->select();

        } catch (\Exception $e) {
            return false;
        }
    }


    public function insert_draw($param, $user)
    {
        try {

            // if(!$user['pay_password']){
            //     return json(['code' => -1, 'msg' => '请先前往安全中心设置支付密码']);
            // }
            // $cost_time = explode(',',$cost_time);
            // $his = date('H:i:s');
            // if($his < $cost_time[0] || $his > $cost_time[1]){
            //     return json(['code' => -1, 'msg' => '提现时间为：'.$cost_time[0] . '~'.$cost_time[1]]);
            // }
            if(config('draw_open')==0){
                    return json(['code' => -1, 'msg' => '不可提现']);

            }
            $account = 'balance';
            $validate = validate('Common');
            if ($param['way'] == '银行卡') {
                // if (!$validate->scene('draw')->check($param)) {
                //     return json(['code' => -1, 'msg' => $validate->getError()]);
                // }
                if(!$user['bank_name'] || !$user['bank_account'] || !$user['bank_number']){
                    return json(['code' => -1, 'msg' => '请完善银行信息']);
                }
                $info = serialize([
                    'bank_name' => $user['bank_name'],
                    'bank_account' => $user['bank_account'],
                    'bank_number' => $user['bank_number']
                ]);
            }elseif($param['way']=='USDT'){
				if (!$user['usdt_addr']) return json(['code' => -1, 'msg' => '请完善钱包地址']);
				$info = serialize([
					'usdt_url' => $param['usdt_addr']
				]);
            }elseif($param['way']=='支付宝'){
				if(!$user['alipay_name'] || !$user['alipay_img']) return json(['code' => -1, 'msg' => '请完善支付宝收款方式']);
				$info = serialize(['alipay_img'=>$user['alipay_img'],'alipay_realname'=>$user['alipay_name']]);
			}else{
                if(!$user['wechat_name'] || !$user['wechat_img']) return json(['code' => -1, 'msg' => '请完善微信收款方式']);
				$info = serialize(['wechat_img'=>$user['wechat_img'],'wechat_name'=>$user['wechat_name']]);
            }

            if ($param['amount'] < config('draw_min') || $param['amount'] % config('draw_bs') != 0) {
                return json(['code' => -1, 'msg' => '提现金额错误<br/>提现最低金额:' . config('draw_min') . '<br/>提现倍数:' . config('draw_bs') . '']);
            }
            if ($user[$account] < $param['amount']) {
                return json(['code' => -1, 'msg' => $param['account'] . '余额不足']);
            }
            //验证验证码
            // $is = $this->check_code($param['code'], $user['mobile']);
            // if ($is['code'] != 1) {
            //     return json(['code' => $is['code'], 'msg' => $is['msg']]);
            // }
            // if (md5($param['pay_password']) != $user['pay_password']) {
            //     return json(['code' => -2, 'msg' => '支付密码错误，请重新输入']);
            // }
            // if ($account == 'agent_bonus' && $user['is_agent_draw'] != 1) {
            //     return json(['code' => -1, 'msg' => '代理佣金暂不可提现']);
            // }

            $cost = $param['amount'] * config('draw_cost');
            $real_amount = $param['amount'] - $cost;
            $id = parent::insertGetId([
                'uid' => $user['id'],
                'code' => $user['code'],
                'nickname' => $user['name'],
                'type' => $account,
                'account' => $account,
                'total' => $param['amount'],
                'cost' => $cost,
                'amount' => $real_amount,
                'create_time' => time(),
                'info' => $info,
                'way' => $param['way']
            ]);
            $res = Db::name('user')->where(['id' => $user['id']])->dec($account,$param['amount'])->update();
            if(!$res)return json(['code'=>-1,'msg'=>'网络异常，请稍后再试！']);

            $data = Account::adds($user['id'], $account,$param['amount'] ,'提现', '-');
            Logs::adds($user['id'], $user['name'], '提现,金额【' . $param['amount'] . '】,ID【' . $id . '】');

            return json(['code' => 1, 'msg' => '您的提现申请已提交，请耐心等待审核']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    function cancel($id,$uid){
    	//详情
		$info = $this->where(['id'=>$id,'uid'=>$uid])->find();

		if(!$info) return return_message('0','提现记录不存在');
		if($info['state']!='1') return return_message('0','提现所处状态不能撤回');

    	Db::startTrans();
    	try{
			//退钱
			$res = Account::create_Inc($info['uid'],$info['total'],$info['account'],'撤回提现','','+');
			if(!$res){Db::rollback(); return return_message('0','撤回失败','1');}

			//变更状态
			$res = $this->where(['id'=>$info['id']])->update(['is_del'=>'1']);
			if(!$res){Db::rollback();return return_message('0','撤回失败','2');}

			Db::commit();
			return return_message('1','提现申请已撤回');
		}catch(\Exception $e){
    		return return_message('0','撤回失败','0');
		}
	}
}
